package com.leandro.tweet;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Tweetar extends Activity implements OnClickListener
{

	public static final String TAG = TweetProjectApplication.APP_NAME;
	Button buttonTweetar;
	EditText editTextStatus;
	TextView textViewQuantCaracteres;
	TwitterHelper twitterHelper;
	TextWatcher textWatcher;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tweet);

		twitterHelper = ((TweetProjectApplication) getApplication()).getTwitterHelper();
		textViewQuantCaracteres = (TextView) findViewById(R.id.textViewQuantCaracteres);

		textWatcher = new CustomTextWatcher();
		editTextStatus = (EditText) findViewById(R.id.editTextStatus);
		editTextStatus.addTextChangedListener(textWatcher);

		buttonTweetar = (Button) findViewById(R.id.buttonTweetarTweet);
		buttonTweetar.setOnClickListener(this);
	}

	public void onClick(View arg0)
	{
		String status;
		status = editTextStatus.getText().toString();
		twitterHelper.updateStatus(status);
		startActivity(new Intent(this, TimeLineActivity.class).addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));

	}

	@Override
	protected void onResume()
	{
		super.onResume();
		reset();
	}

	private void reset()
	{
		editTextStatus.setText("");
		textViewQuantCaracteres.setText("140");
	}

	private class CustomTextWatcher implements TextWatcher
	{

		public void afterTextChanged(Editable s)
		{
		}

		public void beforeTextChanged(CharSequence s, int start, int count, int after)
		{
		}

		public void onTextChanged(CharSequence s, int start, int before, int count)
		{
			int quantDigitado;
			quantDigitado = editTextStatus.getText().toString().length();
			textViewQuantCaracteres.setText((140 - quantDigitado) + "");
		}

	}
}
